package com.sm.honkon.ui.fragment.clients_management

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import com.sm.honkon.R
import com.sm.honkon.app.base.BaseFragment
import com.sm.honkon.app.ext.goBackHome
import com.sm.honkon.app.ext.goMannger
import com.sm.honkon.databinding.FrgClientsManagerBinding
import com.sm.honkon.databinding.FrgSignLoginBinding
import com.sm.honkon.ui.adapter.CustomerAdapter
import com.sm.honkon.ui.model.ClientsManagementViewModel
import com.sm.honkon.ui.model.SignUseLoginViewModel
import kotlinx.android.synthetic.main.frg_clients_manager.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

class ClientsManagementFragment: BaseFragment<ClientsManagementViewModel, FrgClientsManagerBinding>()  {
    override fun layoutId(): Int  = R.layout.frg_clients_manager
    val adapter by lazy {
        CustomerAdapter().apply {
            addChildClickViewIds(R.id.tv_info,R.id.tv_handle)
            setOnItemChildClickListener { adapter, view, position ->
                when(view.id){
                    R.id.tv_handle->{
                        nav().navigateAction(R.id.action_clientsManagementFragment_to_controlLogCatFragment,
                            bundleOf("userId" to data[position].uid)
                        )
                    }
                    R.id.tv_info->{
                        nav().navigateAction(R.id.action_clientsManagementFragment_to_personInfoFragment,
                            bundleOf("userId" to data[position].uid)
                        )
                    }
                }
            }
        }
    }
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.click = Click()
        recycler_view_content.apply {
            adapter = this@ClientsManagementFragment.adapter
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.data.observe(this){
            adapter.setNewInstance(it.toMutableList())
        }
    }

    inner class Click {
        fun add() {
            nav().navigateAction(R.id.action_clientsManagementFragment_to_handleClientsInfoFragment)
        }

        fun back() {
            nav().navigateUp()
        }

        fun home(){
            nav().goBackHome()
        }
        fun mannger(){
        }
        fun exit() {
            System.exit(0)
        }

        fun setting() {
            nav().navigate(R.id.settingFragment)
        }
    }
}